package cc.xsubj.trading.entity;
import cc.xsubj.engine.ResourceManager;

import org.newdawn.slick.Image;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.tiled.TiledMap;

import cc.xsubj.trading.messages.TextOnlyMessage;
import cc.xsubj.trading.utilities.Direction;
import cc.xsubj.trading.world.TiledWorld;


public class TalkingNPC extends NPC {

	String[] lines;
	int currentLine = 0;
	
	public TalkingNPC(TiledMap map, int groupID, int objectID) {
		super(map, groupID, objectID);
		lines = map.getObjectProperty(groupID, objectID, "dialogue", "").split("\\|");
		String spriteSheetName = map.getObjectProperty(groupID, objectID, "sheet", "wizard_sheet");
		
		SpriteSheet sheet = ResourceManager.getSpriteSheet(spriteSheetName);
		sheet.setFilter(Image.FILTER_NEAREST);
		setupAnimations(sheet);
		
	}

	private void setupAnimations(SpriteSheet sheet) {
		// Make this better, merge it with Wizard's.
		setGraphic(sheet);
		addAnimation("DOWN", false, 0, 0);
		addAnimation("UP", false, 0, 1);
		addAnimation("RIGHT", false, 0, 2);
		addAnimation("LEFT", false, 0, 3);
	}

	@Override
	public void interact() {
		String line = lines[currentLine];
		
		this.say(line);
		
		Direction d = ((TiledWorld)world).getPlayerDirection();
		
		switch(d)
		{
		case UP:
		    setAnim("DOWN"); break;
		case DOWN:
		    setAnim("UP"); break;
		case LEFT:
		    setAnim("RIGHT"); break;
		case RIGHT:
		    setAnim("LEFT"); break;
		}
		
		currentLine++;
		if (currentLine >= lines.length)
		{
			currentLine = 0;
		}

	}

	private void say(String text) {
	    TiledWorld t = ((TiledWorld)world);
	    for (String line : TextOnlyMessage.splitText(text))
	    {
	        t.addMessage(new TextOnlyMessage(name, line));
	    }
	    t.addMessage(new TextOnlyMessage(name, "Woop woop!"));
	}

}
